<?php
/**
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 */
/**
 * @param $image
 * 转换为真是的image路径
 */
function get_image($image){
  return   config('filesystems.disks')[config('admin.upload.disk')]['url'].'/'.$image;
}

function getBankImg($bank_id){
    $url = 'https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo='.$bank_id.'&cardBinCheck=true';
    $res = json_decode(curl_get_contents($url),true);
    if(isset($res['bank'])){
        return "https://apimg.alipay.com/combo.png?d=cashier&t=".$res['bank'];
    }else{
        return "";
    }
}
function getBank($bank_id){
    $url = 'https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo='.$bank_id.'&cardBinCheck=true';
    $res = json_decode(curl_get_contents($url),true);
    if(isset($res['bank'])){
        return $res;
    }else{
        throw new \App\Exceptions\Interfaces\OrderServiceException('查询不到银行卡号信息');
    }
}
function curl_get_contents($url,$timeout=1) {
    $curlHandle = curl_init();
    curl_setopt( $curlHandle , CURLOPT_URL, $url );
    curl_setopt( $curlHandle , CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $curlHandle , CURLOPT_TIMEOUT, $timeout );
    $result = curl_exec( $curlHandle );
    curl_close( $curlHandle );
    return $result;
}
//判断是否是微信
function is_weixin(){
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {  //strpos() 函数查找字符串在另一字符串中第一次出现的位置。
        return true;
    }
    return false;
}
function qrcode(){
   return  \Cache::remember('qrcode','2',function(){
        $customer= \App\CustomerConfig::find(1);
       return  get_image($customer->name);
    });
}
function resize_img($url,$path='./'){
    $imgname = $path.uniqid().'.jpg';
    $file = $url;

    list($width, $height) = getimagesize($file); //获取原图尺寸
    $percent = (110/$width);
    //缩放尺寸
    $newwidth = $width * $percent;
    $newheight = $height * $percent;
    $src_im = imagecreatefromjpeg($file);
    $dst_im = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    imagejpeg($dst_im, $imgname); //输出压缩后的图片
    imagedestroy($dst_im);
    imagedestroy($src_im);
    return $imgname;
}
//解决中文乱码问题
function to_unicode($string)
{
    $str = mb_convert_encoding($string, 'UCS-2', 'UTF-8');
    $arrstr = str_split($str, 2);
    $unistr = '';
    foreach ($arrstr as $n) {
        $dec = hexdec(bin2hex($n));
        $unistr .= '&#' . $dec . ';';
    }
    return $unistr;
}